<%@ page import="com.hc.commodity.pojo.Commodity" %>
<%@ page import="com.hc.commodity.dao.impl.CommodityDaoImpl" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %><%--
    将商品添加至购物车中
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2021/4/7
  Time: 16:11
  To change this template use File | Settings | File Templates.
--%>
<%
    //获取要加入购物车的商品id
    int cid = Integer.parseInt(request.getParameter("cid"));
    //获取商品集合
    List<Commodity> commodityList = new CommodityDaoImpl().findAll();
    //获取购物车集合进行添加，如果第一次获取就创建
    List<Commodity> shoppingTrolley = (List<Commodity>) session.getAttribute("shoppingTrolley");
    if (shoppingTrolley == null){
        shoppingTrolley = new ArrayList<Commodity>();
        shoppingTrolley.add(commodityList.get(cid-1));
    }else {
        shoppingTrolley.add(commodityList.get(cid-1));
    }
    session.setAttribute("shoppingTrolley",shoppingTrolley);
    request.getRequestDispatcher("commodity.jsp").forward(request,response);

%>
